<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>module Open3 - open3: Ruby Standard Library Documentation</title>


<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>

<script src="./js/jquery-3.2.0.min.js"></script>

<script src="./js/vue.min.js"></script>
<script src="./js/js.cookie.min.js"></script>

<link href="./css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="./css/rdoc.css" rel="stylesheet">
<link href="./css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
  var index_rel_prefix = "./";
  var darkModeCsseHref = "./css/rdoc-dm.css"
  var defaultModeCssHref = "./css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="module">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0.preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="./index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="./table_of_contents.html#pages">Pages</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    

    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
        
        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-c-capture2">::capture2</a>
    <li ><a href="#method-c-capture2e">::capture2e</a>
    <li ><a href="#method-c-capture3">::capture3</a>
    <li ><a href="#method-c-pipeline">::pipeline</a>
    <li ><a href="#method-c-pipeline_r">::pipeline_r</a>
    <li ><a href="#method-c-pipeline_rw">::pipeline_rw</a>
    <li ><a href="#method-c-pipeline_start">::pipeline_start</a>
    <li ><a href="#method-c-pipeline_w">::pipeline_w</a>
    <li ><a href="#method-c-popen2">::popen2</a>
    <li ><a href="#method-c-popen2e">::popen2e</a>
    <li ><a href="#method-c-popen3">::popen3</a>
    <li ><a href="#method-i-capture2">#capture2</a>
    <li ><a href="#method-i-capture2e">#capture2e</a>
    <li ><a href="#method-i-capture3">#capture3</a>
    <li ><a href="#method-i-pipeline">#pipeline</a>
    <li ><a href="#method-i-pipeline_r">#pipeline_r</a>
    <li ><a href="#method-i-pipeline_rw">#pipeline_rw</a>
    <li ><a href="#method-i-pipeline_start">#pipeline_start</a>
    <li ><a href="#method-i-pipeline_w">#pipeline_w</a>
    <li ><a href="#method-i-popen2">#popen2</a>
    <li ><a href="#method-i-popen2e">#popen2e</a>
    <li ><a href="#method-i-popen3">#popen3</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="module-Open3">
    <h1 id="module-Open3" class="module">
      module Open3
    </h1>

    <section class="description">
    
    </section>

      <section id="5Buntitled-5D" class="documentation-section">


      <section class="constants-list">
      <header>
      <h3>Constants</h3>
      </header>
      <dl>
          <dt id="VERSION">VERSION
          <dd>
        </dl>
        </section>



                <section id="public-class-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Class Methods</h3>
                </header>

                  <div id="method-c-capture2" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">capture2</span><span
                                class="method-args">(*cmd)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-capture2"><code>Open3.capture2</code></a> captures the standard output of a command.</p>

<pre>stdout_str, status = Open3.capture2([env,] cmd... [, opts])</pre>

<p>The arguments env, cmd and opts are passed to <a href="Open3.html#method-c-popen3"><code>Open3.popen3</code></a> except <code>opts[:stdin_data]</code> and <code>opts[:binmode]</code>.  See Process.spawn.</p>

<p>If <code>opts[:stdin_data]</code> is specified, it is sent to the command’s standard input.</p>

<p>If <code>opts[:binmode]</code> is true, internal pipes are set to binary mode.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-comment"># factor is a command for integer factorization.</span>
<span class="ruby-identifier">o</span>, <span class="ruby-identifier">s</span> = <span class="ruby-constant">Open3</span>.<span class="ruby-identifier">capture2</span>(<span class="ruby-string">&quot;factor&quot;</span>, <span class="ruby-value">:stdin_data</span><span class="ruby-operator">=&gt;</span><span class="ruby-string">&quot;42&quot;</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">o</span> <span class="ruby-comment">#=&gt; &quot;42: 2 3 7\n&quot;</span>

<span class="ruby-comment"># generate x**2 graph in png using gnuplot.</span>
<span class="ruby-identifier">gnuplot_commands</span> = <span class="ruby-identifier">&lt;&lt;&quot;End&quot;</span>
<span class="ruby-value">  set terminal png
  plot x**2, &quot;-&quot; with lines
  1 14
  2 1
  3 8
  4 5
  e
</span><span class="ruby-identifier">End</span>
<span class="ruby-identifier">image</span>, <span class="ruby-identifier">s</span> = <span class="ruby-constant">Open3</span>.<span class="ruby-identifier">capture2</span>(<span class="ruby-string">&quot;gnuplot&quot;</span>, <span class="ruby-value">:stdin_data</span><span class="ruby-operator">=&gt;</span><span class="ruby-identifier">gnuplot_commands</span>, <span class="ruby-value">:binmode</span><span class="ruby-operator">=&gt;</span><span class="ruby-keyword">true</span>)
</pre>

                              <div class="method-source-code" id="capture2-source">
            <pre><span class="ruby-comment"># File open3.rb, line 341</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">capture2</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">stdin_data</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:stdin_data</span>)
  <span class="ruby-identifier">binmode</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:binmode</span>)

  <span class="ruby-identifier">popen2</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>, <span class="ruby-identifier">opts</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">o</span>, <span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">binmode</span>
      <span class="ruby-identifier">i</span>.<span class="ruby-identifier">binmode</span>
      <span class="ruby-identifier">o</span>.<span class="ruby-identifier">binmode</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">out_reader</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> { <span class="ruby-identifier">o</span>.<span class="ruby-identifier">read</span> }
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">stdin_data</span>
      <span class="ruby-keyword">begin</span>
        <span class="ruby-keyword">if</span> <span class="ruby-identifier">stdin_data</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:readpartial</span>
          <span class="ruby-constant">IO</span>.<span class="ruby-identifier">copy_stream</span>(<span class="ruby-identifier">stdin_data</span>, <span class="ruby-identifier">i</span>)
        <span class="ruby-keyword">else</span>
          <span class="ruby-identifier">i</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">stdin_data</span>
        <span class="ruby-keyword">end</span>
      <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">EPIPE</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">i</span>.<span class="ruby-identifier">close</span>
    [<span class="ruby-identifier">out_reader</span>.<span class="ruby-identifier">value</span>, <span class="ruby-identifier">t</span>.<span class="ruby-identifier">value</span>]
  }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-capture2e" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">capture2e</span><span
                                class="method-args">(*cmd)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-capture2e"><code>Open3.capture2e</code></a> captures the standard output and the standard error of a command.</p>

<pre>stdout_and_stderr_str, status = Open3.capture2e([env,] cmd... [, opts])</pre>

<p>The arguments env, cmd and opts are passed to <a href="Open3.html#method-c-popen3"><code>Open3.popen3</code></a> except <code>opts[:stdin_data]</code> and <code>opts[:binmode]</code>.  See Process.spawn.</p>

<p>If <code>opts[:stdin_data]</code> is specified, it is sent to the command’s standard input.</p>

<p>If <code>opts[:binmode]</code> is true, internal pipes are set to binary mode.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-comment"># capture make log</span>
<span class="ruby-identifier">make_log</span>, <span class="ruby-identifier">s</span> = <span class="ruby-constant">Open3</span>.<span class="ruby-identifier">capture2e</span>(<span class="ruby-string">&quot;make&quot;</span>)
</pre>

                              <div class="method-source-code" id="capture2e-source">
            <pre><span class="ruby-comment"># File open3.rb, line 389</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">capture2e</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">stdin_data</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:stdin_data</span>)
  <span class="ruby-identifier">binmode</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:binmode</span>)

  <span class="ruby-identifier">popen2e</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>, <span class="ruby-identifier">opts</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">oe</span>, <span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">binmode</span>
      <span class="ruby-identifier">i</span>.<span class="ruby-identifier">binmode</span>
      <span class="ruby-identifier">oe</span>.<span class="ruby-identifier">binmode</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">outerr_reader</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> { <span class="ruby-identifier">oe</span>.<span class="ruby-identifier">read</span> }
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">stdin_data</span>
      <span class="ruby-keyword">begin</span>
        <span class="ruby-keyword">if</span> <span class="ruby-identifier">stdin_data</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:readpartial</span>
          <span class="ruby-constant">IO</span>.<span class="ruby-identifier">copy_stream</span>(<span class="ruby-identifier">stdin_data</span>, <span class="ruby-identifier">i</span>)
        <span class="ruby-keyword">else</span>
          <span class="ruby-identifier">i</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">stdin_data</span>
        <span class="ruby-keyword">end</span>
      <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">EPIPE</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">i</span>.<span class="ruby-identifier">close</span>
    [<span class="ruby-identifier">outerr_reader</span>.<span class="ruby-identifier">value</span>, <span class="ruby-identifier">t</span>.<span class="ruby-identifier">value</span>]
  }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-capture3" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">capture3</span><span
                                class="method-args">(*cmd)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-capture3"><code>Open3.capture3</code></a> captures the standard output and the standard error of a command.</p>

<pre>stdout_str, stderr_str, status = Open3.capture3([env,] cmd... [, opts])</pre>

<p>The arguments env, cmd and opts are passed to <a href="Open3.html#method-c-popen3"><code>Open3.popen3</code></a> except <code>opts[:stdin_data]</code> and <code>opts[:binmode]</code>.  See Process.spawn.</p>

<p>If <code>opts[:stdin_data]</code> is specified, it is sent to the command’s standard input.</p>

<p>If <code>opts[:binmode]</code> is true, internal pipes are set to binary mode.</p>

<p>Examples:</p>

<pre class="ruby"><span class="ruby-comment"># dot is a command of graphviz.</span>
<span class="ruby-identifier">graph</span> = <span class="ruby-identifier">&lt;&lt;&#39;End&#39;</span>
<span class="ruby-value">  digraph g {
    a -&gt; b
  }
</span><span class="ruby-identifier">End</span>
<span class="ruby-identifier">drawn_graph</span>, <span class="ruby-identifier">dot_log</span> = <span class="ruby-constant">Open3</span>.<span class="ruby-identifier">capture3</span>(<span class="ruby-string">&quot;dot -v&quot;</span>, <span class="ruby-value">:stdin_data</span><span class="ruby-operator">=&gt;</span><span class="ruby-identifier">graph</span>)

<span class="ruby-identifier">o</span>, <span class="ruby-identifier">e</span>, <span class="ruby-identifier">s</span> = <span class="ruby-constant">Open3</span>.<span class="ruby-identifier">capture3</span>(<span class="ruby-string">&quot;echo abc; sort &gt;&amp;2&quot;</span>, <span class="ruby-value">:stdin_data</span><span class="ruby-operator">=&gt;</span><span class="ruby-string">&quot;foo\nbar\nbaz\n&quot;</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">o</span> <span class="ruby-comment">#=&gt; &quot;abc\n&quot;</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span> <span class="ruby-comment">#=&gt; &quot;bar\nbaz\nfoo\n&quot;</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s</span> <span class="ruby-comment">#=&gt; #&lt;Process::Status: pid 32682 exit 0&gt;</span>

<span class="ruby-comment"># generate a thumbnail image using the convert command of ImageMagick.</span>
<span class="ruby-comment"># However, if the image is really stored in a file,</span>
<span class="ruby-comment"># system(&quot;convert&quot;, &quot;-thumbnail&quot;, &quot;80&quot;, &quot;png:#{filename}&quot;, &quot;png:-&quot;) is better</span>
<span class="ruby-comment"># because of reduced memory consumption.</span>
<span class="ruby-comment"># But if the image is stored in a DB or generated by the gnuplot Open3.capture2 example,</span>
<span class="ruby-comment"># Open3.capture3 should be considered.</span>
<span class="ruby-comment">#</span>
<span class="ruby-identifier">image</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span>(<span class="ruby-string">&quot;/usr/share/openclipart/png/animals/mammals/sheep-md-v0.1.png&quot;</span>, <span class="ruby-value">:binmode</span><span class="ruby-operator">=&gt;</span><span class="ruby-keyword">true</span>)
<span class="ruby-identifier">thumbnail</span>, <span class="ruby-identifier">err</span>, <span class="ruby-identifier">s</span> = <span class="ruby-constant">Open3</span>.<span class="ruby-identifier">capture3</span>(<span class="ruby-string">&quot;convert -thumbnail 80 png:- png:-&quot;</span>, <span class="ruby-value">:stdin_data</span><span class="ruby-operator">=&gt;</span><span class="ruby-identifier">image</span>, <span class="ruby-value">:binmode</span><span class="ruby-operator">=&gt;</span><span class="ruby-keyword">true</span>)
<span class="ruby-keyword">if</span> <span class="ruby-identifier">s</span>.<span class="ruby-identifier">success?</span>
  <span class="ruby-constant">STDOUT</span>.<span class="ruby-identifier">binmode</span>; <span class="ruby-identifier">print</span> <span class="ruby-identifier">thumbnail</span>
<span class="ruby-keyword">end</span>
</pre>

                              <div class="method-source-code" id="capture3-source">
            <pre><span class="ruby-comment"># File open3.rb, line 280</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">capture3</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">stdin_data</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:stdin_data</span>) <span class="ruby-operator">||</span> <span class="ruby-string">&#39;&#39;</span>
  <span class="ruby-identifier">binmode</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:binmode</span>)

  <span class="ruby-identifier">popen3</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>, <span class="ruby-identifier">opts</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">o</span>, <span class="ruby-identifier">e</span>, <span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">binmode</span>
      <span class="ruby-identifier">i</span>.<span class="ruby-identifier">binmode</span>
      <span class="ruby-identifier">o</span>.<span class="ruby-identifier">binmode</span>
      <span class="ruby-identifier">e</span>.<span class="ruby-identifier">binmode</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">out_reader</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> { <span class="ruby-identifier">o</span>.<span class="ruby-identifier">read</span> }
    <span class="ruby-identifier">err_reader</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> { <span class="ruby-identifier">e</span>.<span class="ruby-identifier">read</span> }
    <span class="ruby-keyword">begin</span>
      <span class="ruby-keyword">if</span> <span class="ruby-identifier">stdin_data</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:readpartial</span>
        <span class="ruby-constant">IO</span>.<span class="ruby-identifier">copy_stream</span>(<span class="ruby-identifier">stdin_data</span>, <span class="ruby-identifier">i</span>)
      <span class="ruby-keyword">else</span>
        <span class="ruby-identifier">i</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">stdin_data</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">EPIPE</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">i</span>.<span class="ruby-identifier">close</span>
    [<span class="ruby-identifier">out_reader</span>.<span class="ruby-identifier">value</span>, <span class="ruby-identifier">err_reader</span>.<span class="ruby-identifier">value</span>, <span class="ruby-identifier">t</span>.<span class="ruby-identifier">value</span>]
  }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-pipeline" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">pipeline</span><span
                                class="method-args">(*cmds)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-pipeline"><code>Open3.pipeline</code></a> starts a list of commands as a pipeline. It waits for the completion of the commands. No pipes are created for stdin of the first command and stdout of the last command.</p>

<pre>status_list = Open3.pipeline(cmd1, cmd2, ... [, opts])</pre>

<p>Each cmd is a string or an array. If it is an array, the elements are passed to Process.spawn.</p>

<pre>cmd:
  commandline                              command line string which is passed to a shell
  [env, commandline, opts]                 command line string which is passed to a shell
  [env, cmdname, arg1, ..., opts]          command name and one or more arguments (no shell)
  [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)

Note that env and opts are optional, as Process.spawn.</pre>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">fname</span> = <span class="ruby-string">&quot;/usr/share/man/man1/ruby.1.gz&quot;</span>
<span class="ruby-identifier">p</span> <span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline</span>([<span class="ruby-string">&quot;zcat&quot;</span>, <span class="ruby-identifier">fname</span>], <span class="ruby-string">&quot;nroff -man&quot;</span>, <span class="ruby-string">&quot;less&quot;</span>)
<span class="ruby-comment">#=&gt; [#&lt;Process::Status: pid 11817 exit 0&gt;,</span>
<span class="ruby-comment">#    #&lt;Process::Status: pid 11820 exit 0&gt;,</span>
<span class="ruby-comment">#    #&lt;Process::Status: pid 11828 exit 0&gt;]</span>

<span class="ruby-identifier">fname</span> = <span class="ruby-string">&quot;/usr/share/man/man1/ls.1.gz&quot;</span>
<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline</span>([<span class="ruby-string">&quot;zcat&quot;</span>, <span class="ruby-identifier">fname</span>], <span class="ruby-string">&quot;nroff -man&quot;</span>, <span class="ruby-string">&quot;colcrt&quot;</span>)

<span class="ruby-comment"># convert PDF to PS and send to a printer by lpr</span>
<span class="ruby-identifier">pdf_file</span> = <span class="ruby-string">&quot;paper.pdf&quot;</span>
<span class="ruby-identifier">printer</span> = <span class="ruby-string">&quot;printer-name&quot;</span>
<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline</span>([<span class="ruby-string">&quot;pdftops&quot;</span>, <span class="ruby-identifier">pdf_file</span>, <span class="ruby-string">&quot;-&quot;</span>],
               [<span class="ruby-string">&quot;lpr&quot;</span>, <span class="ruby-node">&quot;-P#{printer}&quot;</span>])

<span class="ruby-comment"># count lines</span>
<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline</span>(<span class="ruby-string">&quot;sort&quot;</span>, <span class="ruby-string">&quot;uniq -c&quot;</span>, <span class="ruby-value">:in</span><span class="ruby-operator">=&gt;</span><span class="ruby-string">&quot;names.txt&quot;</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=&gt;</span><span class="ruby-string">&quot;count&quot;</span>)

<span class="ruby-comment"># cyclic pipeline</span>
<span class="ruby-identifier">r</span>,<span class="ruby-identifier">w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
<span class="ruby-identifier">w</span>.<span class="ruby-identifier">print</span> <span class="ruby-string">&quot;ibase=14\n10\n&quot;</span>
<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline</span>(<span class="ruby-string">&quot;bc&quot;</span>, <span class="ruby-string">&quot;tee /dev/tty&quot;</span>, <span class="ruby-value">:in</span><span class="ruby-operator">=&gt;</span><span class="ruby-identifier">r</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=&gt;</span><span class="ruby-identifier">w</span>)
<span class="ruby-comment">#=&gt; 14</span>
<span class="ruby-comment">#   18</span>
<span class="ruby-comment">#   22</span>
<span class="ruby-comment">#   30</span>
<span class="ruby-comment">#   42</span>
<span class="ruby-comment">#   58</span>
<span class="ruby-comment">#   78</span>
<span class="ruby-comment">#   106</span>
<span class="ruby-comment">#   202</span>
</pre>

                              <div class="method-source-code" id="pipeline-source">
            <pre><span class="ruby-comment"># File open3.rb, line 688</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pipeline</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmds</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">pipeline_run</span>(<span class="ruby-identifier">cmds</span>, <span class="ruby-identifier">opts</span>, [], []) {<span class="ruby-operator">|</span><span class="ruby-identifier">ts</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">ts</span>.<span class="ruby-identifier">map</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:value</span>)
  }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-pipeline_r" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">pipeline_r</span><span
                                class="method-args">(*cmds, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-pipeline_r"><code>Open3.pipeline_r</code></a> starts a list of commands as a pipeline with a pipe which connects to stdout of the last command.</p>

<pre>Open3.pipeline_r(cmd1, cmd2, ... [, opts]) {|last_stdout, wait_threads|
  ...
}

last_stdout, wait_threads = Open3.pipeline_r(cmd1, cmd2, ... [, opts])
...
last_stdout.close</pre>

<p>Each cmd is a string or an array. If it is an array, the elements are passed to Process.spawn.</p>

<pre>cmd:
  commandline                              command line string which is passed to a shell
  [env, commandline, opts]                 command line string which is passed to a shell
  [env, cmdname, arg1, ..., opts]          command name and one or more arguments (no shell)
  [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)

Note that env and opts are optional, as for Process.spawn.</pre>

<p>Example:</p>

<pre>Open3.pipeline_r(&quot;zcat /var/log/apache2/access.log.*.gz&quot;,
                 [{&quot;LANG&quot;=&gt;&quot;C&quot;}, &quot;grep&quot;, &quot;GET /favicon.ico&quot;],
                 &quot;logresolve&quot;) {|o, ts|
  o.each_line {|line|
    ...
  }
}

Open3.pipeline_r(&quot;yes&quot;, &quot;head -10&quot;) {|o, ts|
  p o.read      #=&gt; &quot;y\ny\ny\ny\ny\ny\ny\ny\ny\ny\n&quot;
  p ts[0].value #=&gt; #&lt;Process::Status: pid 24910 SIGPIPE (signal 13)&gt;
  p ts[1].value #=&gt; #&lt;Process::Status: pid 24913 exit 0&gt;
}</pre>

                              <div class="method-source-code" id="pipeline_r-source">
            <pre><span class="ruby-comment"># File open3.rb, line 519</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pipeline_r</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmds</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">out_r</span>, <span class="ruby-identifier">out_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:out</span>] = <span class="ruby-identifier">out_w</span>

  <span class="ruby-identifier">pipeline_run</span>(<span class="ruby-identifier">cmds</span>, <span class="ruby-identifier">opts</span>, [<span class="ruby-identifier">out_w</span>], [<span class="ruby-identifier">out_r</span>], <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-pipeline_rw" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">pipeline_rw</span><span
                                class="method-args">(*cmds, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-pipeline_rw"><code>Open3.pipeline_rw</code></a> starts a list of commands as a pipeline with pipes which connect to stdin of the first command and stdout of the last command.</p>

<pre>Open3.pipeline_rw(cmd1, cmd2, ... [, opts]) {|first_stdin, last_stdout, wait_threads|
  ...
}

first_stdin, last_stdout, wait_threads = Open3.pipeline_rw(cmd1, cmd2, ... [, opts])
...
first_stdin.close
last_stdout.close</pre>

<p>Each cmd is a string or an array. If it is an array, the elements are passed to Process.spawn.</p>

<pre>cmd:
  commandline                              command line string which is passed to a shell
  [env, commandline, opts]                 command line string which is passed to a shell
  [env, cmdname, arg1, ..., opts]          command name and one or more arguments (no shell)
  [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)

Note that env and opts are optional, as for Process.spawn.</pre>

<p>The options to pass to Process.spawn are constructed by merging <code>opts</code>, the last hash element of the array, and specifications for the pipes between each of the commands.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline_rw</span>(<span class="ruby-string">&quot;tr -dc A-Za-z&quot;</span>, <span class="ruby-string">&quot;wc -c&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">o</span>, <span class="ruby-identifier">ts</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;All persons more than a mile high to leave the court.&quot;</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">close</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">o</span>.<span class="ruby-identifier">gets</span> <span class="ruby-comment">#=&gt; &quot;42\n&quot;</span>
}

<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline_rw</span>(<span class="ruby-string">&quot;sort&quot;</span>, <span class="ruby-string">&quot;cat -n&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">stdin</span>, <span class="ruby-identifier">stdout</span>, <span class="ruby-identifier">wait_thrs</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">stdin</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;foo&quot;</span>
  <span class="ruby-identifier">stdin</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;bar&quot;</span>
  <span class="ruby-identifier">stdin</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;baz&quot;</span>
  <span class="ruby-identifier">stdin</span>.<span class="ruby-identifier">close</span>     <span class="ruby-comment"># send EOF to sort.</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">stdout</span>.<span class="ruby-identifier">read</span>   <span class="ruby-comment">#=&gt; &quot;     1\tbar\n     2\tbaz\n     3\tfoo\n&quot;</span>
}
</pre>

                              <div class="method-source-code" id="pipeline_rw-source">
            <pre><span class="ruby-comment"># File open3.rb, line 463</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pipeline_rw</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmds</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">in_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:in</span>] = <span class="ruby-identifier">in_r</span>
  <span class="ruby-identifier">in_w</span>.<span class="ruby-identifier">sync</span> = <span class="ruby-keyword">true</span>

  <span class="ruby-identifier">out_r</span>, <span class="ruby-identifier">out_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:out</span>] = <span class="ruby-identifier">out_w</span>

  <span class="ruby-identifier">pipeline_run</span>(<span class="ruby-identifier">cmds</span>, <span class="ruby-identifier">opts</span>, [<span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">out_w</span>], [<span class="ruby-identifier">in_w</span>, <span class="ruby-identifier">out_r</span>], <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-pipeline_start" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">pipeline_start</span><span
                                class="method-args">(*cmds, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-pipeline_start"><code>Open3.pipeline_start</code></a> starts a list of commands as a pipeline. No pipes are created for stdin of the first command and stdout of the last command.</p>

<pre>Open3.pipeline_start(cmd1, cmd2, ... [, opts]) {|wait_threads|
  ...
}

wait_threads = Open3.pipeline_start(cmd1, cmd2, ... [, opts])
...</pre>

<p>Each cmd is a string or an array. If it is an array, the elements are passed to Process.spawn.</p>

<pre>cmd:
  commandline                              command line string which is passed to a shell
  [env, commandline, opts]                 command line string which is passed to a shell
  [env, cmdname, arg1, ..., opts]          command name and one or more arguments (no shell)
  [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)

Note that env and opts are optional, as for Process.spawn.</pre>

<p>Example:</p>

<pre class="ruby"><span class="ruby-comment"># Run xeyes in 10 seconds.</span>
<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline_start</span>(<span class="ruby-string">&quot;xeyes&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">ts</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">sleep</span> <span class="ruby-value">10</span>
  <span class="ruby-identifier">t</span> = <span class="ruby-identifier">ts</span>[<span class="ruby-value">0</span>]
  <span class="ruby-constant">Process</span>.<span class="ruby-identifier">kill</span>(<span class="ruby-string">&quot;TERM&quot;</span>, <span class="ruby-identifier">t</span>.<span class="ruby-identifier">pid</span>)
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">t</span>.<span class="ruby-identifier">value</span> <span class="ruby-comment">#=&gt; #&lt;Process::Status: pid 911 SIGTERM (signal 15)&gt;</span>
}

<span class="ruby-comment"># Convert pdf to ps and send it to a printer.</span>
<span class="ruby-comment"># Collect error message of pdftops and lpr.</span>
<span class="ruby-identifier">pdf_file</span> = <span class="ruby-string">&quot;paper.pdf&quot;</span>
<span class="ruby-identifier">printer</span> = <span class="ruby-string">&quot;printer-name&quot;</span>
<span class="ruby-identifier">err_r</span>, <span class="ruby-identifier">err_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline_start</span>([<span class="ruby-string">&quot;pdftops&quot;</span>, <span class="ruby-identifier">pdf_file</span>, <span class="ruby-string">&quot;-&quot;</span>],
                     [<span class="ruby-string">&quot;lpr&quot;</span>, <span class="ruby-node">&quot;-P#{printer}&quot;</span>],
                     <span class="ruby-value">:err</span><span class="ruby-operator">=&gt;</span><span class="ruby-identifier">err_w</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">ts</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">err_w</span>.<span class="ruby-identifier">close</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">err_r</span>.<span class="ruby-identifier">read</span> <span class="ruby-comment"># error messages of pdftops and lpr.</span>
}
</pre>

                              <div class="method-source-code" id="pipeline_start-source">
            <pre><span class="ruby-comment"># File open3.rb, line 620</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pipeline_start</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmds</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">block</span>
    <span class="ruby-identifier">pipeline_run</span>(<span class="ruby-identifier">cmds</span>, <span class="ruby-identifier">opts</span>, [], [], <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">ts</span>, = <span class="ruby-identifier">pipeline_run</span>(<span class="ruby-identifier">cmds</span>, <span class="ruby-identifier">opts</span>, [], [])
    <span class="ruby-identifier">ts</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-pipeline_w" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">pipeline_w</span><span
                                class="method-args">(*cmds, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-pipeline_w"><code>Open3.pipeline_w</code></a> starts a list of commands as a pipeline with a pipe which connects to stdin of the first command.</p>

<pre>Open3.pipeline_w(cmd1, cmd2, ... [, opts]) {|first_stdin, wait_threads|
  ...
}

first_stdin, wait_threads = Open3.pipeline_w(cmd1, cmd2, ... [, opts])
...
first_stdin.close</pre>

<p>Each cmd is a string or an array. If it is an array, the elements are passed to Process.spawn.</p>

<pre>cmd:
  commandline                              command line string which is passed to a shell
  [env, commandline, opts]                 command line string which is passed to a shell
  [env, cmdname, arg1, ..., opts]          command name and one or more arguments (no shell)
  [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)

Note that env and opts are optional, as for Process.spawn.</pre>

<p>Example:</p>

<pre class="ruby"><span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline_w</span>(<span class="ruby-string">&quot;bzip2 -c&quot;</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=&gt;</span><span class="ruby-string">&quot;/tmp/hello.bz2&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">ts</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;hello&quot;</span>
}
</pre>

                              <div class="method-source-code" id="pipeline_w-source">
            <pre><span class="ruby-comment"># File open3.rb, line 561</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pipeline_w</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmds</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">in_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:in</span>] = <span class="ruby-identifier">in_r</span>
  <span class="ruby-identifier">in_w</span>.<span class="ruby-identifier">sync</span> = <span class="ruby-keyword">true</span>

  <span class="ruby-identifier">pipeline_run</span>(<span class="ruby-identifier">cmds</span>, <span class="ruby-identifier">opts</span>, [<span class="ruby-identifier">in_r</span>], [<span class="ruby-identifier">in_w</span>], <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-popen2" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">popen2</span><span
                                class="method-args">(*cmd, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-popen2"><code>Open3.popen2</code></a> is similar to <a href="Open3.html#method-c-popen3"><code>Open3.popen3</code></a> except that it doesn’t create a pipe for the standard error stream.</p>

<p>Block form:</p>

<pre>Open3.popen2([env,] cmd... [, opts]) {|stdin, stdout, wait_thr|
  pid = wait_thr.pid # pid of the started process.
  ...
  exit_status = wait_thr.value # Process::Status object returned.
}</pre>

<p>Non-block form:</p>

<pre>stdin, stdout, wait_thr = Open3.popen2([env,] cmd... [, opts])
...
stdin.close  # stdin and stdout should be closed explicitly in this form.
stdout.close</pre>

<p>See Process.spawn for the optional hash arguments <em>env</em> and <em>opts</em>.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-constant">Open3</span>.<span class="ruby-identifier">popen2</span>(<span class="ruby-string">&quot;wc -c&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>,<span class="ruby-identifier">o</span>,<span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">print</span> <span class="ruby-string">&quot;answer to life the universe and everything&quot;</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">close</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">o</span>.<span class="ruby-identifier">gets</span> <span class="ruby-comment">#=&gt; &quot;42\n&quot;</span>
}

<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">popen2</span>(<span class="ruby-string">&quot;bc -q&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>,<span class="ruby-identifier">o</span>,<span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;obase=13&quot;</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;6 * 9&quot;</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">o</span>.<span class="ruby-identifier">gets</span> <span class="ruby-comment">#=&gt; &quot;42\n&quot;</span>
}

<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">popen2</span>(<span class="ruby-string">&quot;dc&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>,<span class="ruby-identifier">o</span>,<span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">print</span> <span class="ruby-string">&quot;42P&quot;</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">close</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">o</span>.<span class="ruby-identifier">read</span> <span class="ruby-comment">#=&gt; &quot;*&quot;</span>
}
</pre>

                              <div class="method-source-code" id="popen2-source">
            <pre><span class="ruby-comment"># File open3.rb, line 147</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">popen2</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">in_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:in</span>] = <span class="ruby-identifier">in_r</span>
  <span class="ruby-identifier">in_w</span>.<span class="ruby-identifier">sync</span> = <span class="ruby-keyword">true</span>

  <span class="ruby-identifier">out_r</span>, <span class="ruby-identifier">out_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:out</span>] = <span class="ruby-identifier">out_w</span>

  <span class="ruby-identifier">popen_run</span>(<span class="ruby-identifier">cmd</span>, <span class="ruby-identifier">opts</span>, [<span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">out_w</span>], [<span class="ruby-identifier">in_w</span>, <span class="ruby-identifier">out_r</span>], <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-popen2e" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">popen2e</span><span
                                class="method-args">(*cmd, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-popen2e"><code>Open3.popen2e</code></a> is similar to <a href="Open3.html#method-c-popen3"><code>Open3.popen3</code></a> except that it merges the standard output stream and the standard error stream.</p>

<p>Block form:</p>

<pre>Open3.popen2e([env,] cmd... [, opts]) {|stdin, stdout_and_stderr, wait_thr|
  pid = wait_thr.pid # pid of the started process.
  ...
  exit_status = wait_thr.value # Process::Status object returned.
}</pre>

<p>Non-block form:</p>

<pre>stdin, stdout_and_stderr, wait_thr = Open3.popen2e([env,] cmd... [, opts])
...
stdin.close  # stdin and stdout_and_stderr should be closed explicitly in this form.
stdout_and_stderr.close</pre>

<p>See Process.spawn for the optional hash arguments <em>env</em> and <em>opts</em>.</p>

<p>Example:</p>

<pre># check gcc warnings
source = &quot;foo.c&quot;
Open3.popen2e(&quot;gcc&quot;, &quot;-Wall&quot;, source) {|i,oe,t|
  oe.each {|line|
    if /warning/ =~ line
      ...
    end
  }
}</pre>

                              <div class="method-source-code" id="popen2e-source">
            <pre><span class="ruby-comment"># File open3.rb, line 196</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">popen2e</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">in_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:in</span>] = <span class="ruby-identifier">in_r</span>
  <span class="ruby-identifier">in_w</span>.<span class="ruby-identifier">sync</span> = <span class="ruby-keyword">true</span>

  <span class="ruby-identifier">out_r</span>, <span class="ruby-identifier">out_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[[<span class="ruby-value">:out</span>, <span class="ruby-value">:err</span>]] = <span class="ruby-identifier">out_w</span>

  <span class="ruby-identifier">popen_run</span>(<span class="ruby-identifier">cmd</span>, <span class="ruby-identifier">opts</span>, [<span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">out_w</span>], [<span class="ruby-identifier">in_w</span>, <span class="ruby-identifier">out_r</span>], <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">ensure</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">block</span>
    <span class="ruby-identifier">in_r</span>.<span class="ruby-identifier">close</span>
    <span class="ruby-identifier">in_w</span>.<span class="ruby-identifier">close</span>
    <span class="ruby-identifier">out_r</span>.<span class="ruby-identifier">close</span>
    <span class="ruby-identifier">out_w</span>.<span class="ruby-identifier">close</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-popen3" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">popen3</span><span
                                class="method-args">(*cmd, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Open stdin, stdout, and stderr streams and start external executable. In addition, a thread to wait for the started process is created. The thread has a pid method and a thread variable :pid which is the pid of the started process.</p>

<p>Block form:</p>

<pre>Open3.popen3([env,] cmd... [, opts]) {|stdin, stdout, stderr, wait_thr|
  pid = wait_thr.pid # pid of the started process.
  ...
  exit_status = wait_thr.value # Process::Status object returned.
}</pre>

<p>Non-block form:</p>

<pre>stdin, stdout, stderr, wait_thr = Open3.popen3([env,] cmd... [, opts])
pid = wait_thr[:pid]  # pid of the started process
...
stdin.close  # stdin, stdout and stderr should be closed explicitly in this form.
stdout.close
stderr.close
exit_status = wait_thr.value  # Process::Status object returned.</pre>

<p>The parameters env, cmd, and opts are passed to Process.spawn. A commandline string and a list of argument strings can be accepted as follows:</p>

<pre>Open3.popen3(&quot;echo abc&quot;) {|i, o, e, t| ... }
Open3.popen3(&quot;echo&quot;, &quot;abc&quot;) {|i, o, e, t| ... }
Open3.popen3([&quot;echo&quot;, &quot;argv0&quot;], &quot;abc&quot;) {|i, o, e, t| ... }</pre>

<p>If the last parameter, opts, is a Hash, it is recognized as an option for Process.spawn.</p>

<pre class="ruby"><span class="ruby-constant">Open3</span>.<span class="ruby-identifier">popen3</span>(<span class="ruby-string">&quot;pwd&quot;</span>, <span class="ruby-value">:chdir</span><span class="ruby-operator">=&gt;</span><span class="ruby-string">&quot;/&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>,<span class="ruby-identifier">o</span>,<span class="ruby-identifier">e</span>,<span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">o</span>.<span class="ruby-identifier">read</span>.<span class="ruby-identifier">chomp</span> <span class="ruby-comment">#=&gt; &quot;/&quot;</span>
}
</pre>

<p>wait_thr.value waits for the termination of the process. The block form also waits for the process when it returns.</p>

<p>Closing stdin, stdout and stderr does not wait for the process to complete.</p>

<p>You should be careful to avoid deadlocks. Since pipes are fixed length buffers, <a href="Open3.html#method-c-popen3"><code>Open3.popen3</code></a>(“prog”) {|i, o, e, t| o.read } deadlocks if the program generates too much output on stderr. You should read stdout and stderr simultaneously (using threads or IO.select). However, if you don’t need stderr output, you can use <a href="Open3.html#method-c-popen2"><code>Open3.popen2</code></a>. If merged stdout and stderr output is not a problem, you can use <a href="Open3.html#method-c-popen2e"><code>Open3.popen2e</code></a>. If you really need stdout and stderr output as separate strings, you can consider <a href="Open3.html#method-c-capture3"><code>Open3.capture3</code></a>.</p>

                              <div class="method-source-code" id="popen3-source">
            <pre><span class="ruby-comment"># File open3.rb, line 86</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">popen3</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">in_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:in</span>] = <span class="ruby-identifier">in_r</span>
  <span class="ruby-identifier">in_w</span>.<span class="ruby-identifier">sync</span> = <span class="ruby-keyword">true</span>

  <span class="ruby-identifier">out_r</span>, <span class="ruby-identifier">out_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:out</span>] = <span class="ruby-identifier">out_w</span>

  <span class="ruby-identifier">err_r</span>, <span class="ruby-identifier">err_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:err</span>] = <span class="ruby-identifier">err_w</span>

  <span class="ruby-identifier">popen_run</span>(<span class="ruby-identifier">cmd</span>, <span class="ruby-identifier">opts</span>, [<span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">out_w</span>, <span class="ruby-identifier">err_w</span>], [<span class="ruby-identifier">in_w</span>, <span class="ruby-identifier">out_r</span>, <span class="ruby-identifier">err_r</span>], <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="private-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Private Instance Methods</h3>
                </header>

                  <div id="method-i-capture2" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">capture2</span><span
                                class="method-args">(*cmd)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-capture2"><code>Open3.capture2</code></a> captures the standard output of a command.</p>

<pre>stdout_str, status = Open3.capture2([env,] cmd... [, opts])</pre>

<p>The arguments env, cmd and opts are passed to <a href="Open3.html#method-c-popen3"><code>Open3.popen3</code></a> except <code>opts[:stdin_data]</code> and <code>opts[:binmode]</code>.  See Process.spawn.</p>

<p>If <code>opts[:stdin_data]</code> is specified, it is sent to the command’s standard input.</p>

<p>If <code>opts[:binmode]</code> is true, internal pipes are set to binary mode.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-comment"># factor is a command for integer factorization.</span>
<span class="ruby-identifier">o</span>, <span class="ruby-identifier">s</span> = <span class="ruby-constant">Open3</span>.<span class="ruby-identifier">capture2</span>(<span class="ruby-string">&quot;factor&quot;</span>, <span class="ruby-value">:stdin_data</span><span class="ruby-operator">=&gt;</span><span class="ruby-string">&quot;42&quot;</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">o</span> <span class="ruby-comment">#=&gt; &quot;42: 2 3 7\n&quot;</span>

<span class="ruby-comment"># generate x**2 graph in png using gnuplot.</span>
<span class="ruby-identifier">gnuplot_commands</span> = <span class="ruby-identifier">&lt;&lt;&quot;End&quot;</span>
<span class="ruby-value">  set terminal png
  plot x**2, &quot;-&quot; with lines
  1 14
  2 1
  3 8
  4 5
  e
</span><span class="ruby-identifier">End</span>
<span class="ruby-identifier">image</span>, <span class="ruby-identifier">s</span> = <span class="ruby-constant">Open3</span>.<span class="ruby-identifier">capture2</span>(<span class="ruby-string">&quot;gnuplot&quot;</span>, <span class="ruby-value">:stdin_data</span><span class="ruby-operator">=&gt;</span><span class="ruby-identifier">gnuplot_commands</span>, <span class="ruby-value">:binmode</span><span class="ruby-operator">=&gt;</span><span class="ruby-keyword">true</span>)
</pre>

                              <div class="method-source-code" id="capture2-source">
            <pre><span class="ruby-comment"># File open3.rb, line 341</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">capture2</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">stdin_data</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:stdin_data</span>)
  <span class="ruby-identifier">binmode</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:binmode</span>)

  <span class="ruby-identifier">popen2</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>, <span class="ruby-identifier">opts</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">o</span>, <span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">binmode</span>
      <span class="ruby-identifier">i</span>.<span class="ruby-identifier">binmode</span>
      <span class="ruby-identifier">o</span>.<span class="ruby-identifier">binmode</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">out_reader</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> { <span class="ruby-identifier">o</span>.<span class="ruby-identifier">read</span> }
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">stdin_data</span>
      <span class="ruby-keyword">begin</span>
        <span class="ruby-keyword">if</span> <span class="ruby-identifier">stdin_data</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:readpartial</span>
          <span class="ruby-constant">IO</span>.<span class="ruby-identifier">copy_stream</span>(<span class="ruby-identifier">stdin_data</span>, <span class="ruby-identifier">i</span>)
        <span class="ruby-keyword">else</span>
          <span class="ruby-identifier">i</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">stdin_data</span>
        <span class="ruby-keyword">end</span>
      <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">EPIPE</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">i</span>.<span class="ruby-identifier">close</span>
    [<span class="ruby-identifier">out_reader</span>.<span class="ruby-identifier">value</span>, <span class="ruby-identifier">t</span>.<span class="ruby-identifier">value</span>]
  }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-capture2e" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">capture2e</span><span
                                class="method-args">(*cmd)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-capture2e"><code>Open3.capture2e</code></a> captures the standard output and the standard error of a command.</p>

<pre>stdout_and_stderr_str, status = Open3.capture2e([env,] cmd... [, opts])</pre>

<p>The arguments env, cmd and opts are passed to <a href="Open3.html#method-c-popen3"><code>Open3.popen3</code></a> except <code>opts[:stdin_data]</code> and <code>opts[:binmode]</code>.  See Process.spawn.</p>

<p>If <code>opts[:stdin_data]</code> is specified, it is sent to the command’s standard input.</p>

<p>If <code>opts[:binmode]</code> is true, internal pipes are set to binary mode.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-comment"># capture make log</span>
<span class="ruby-identifier">make_log</span>, <span class="ruby-identifier">s</span> = <span class="ruby-constant">Open3</span>.<span class="ruby-identifier">capture2e</span>(<span class="ruby-string">&quot;make&quot;</span>)
</pre>

                              <div class="method-source-code" id="capture2e-source">
            <pre><span class="ruby-comment"># File open3.rb, line 389</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">capture2e</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">stdin_data</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:stdin_data</span>)
  <span class="ruby-identifier">binmode</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:binmode</span>)

  <span class="ruby-identifier">popen2e</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>, <span class="ruby-identifier">opts</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">oe</span>, <span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">binmode</span>
      <span class="ruby-identifier">i</span>.<span class="ruby-identifier">binmode</span>
      <span class="ruby-identifier">oe</span>.<span class="ruby-identifier">binmode</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">outerr_reader</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> { <span class="ruby-identifier">oe</span>.<span class="ruby-identifier">read</span> }
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">stdin_data</span>
      <span class="ruby-keyword">begin</span>
        <span class="ruby-keyword">if</span> <span class="ruby-identifier">stdin_data</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:readpartial</span>
          <span class="ruby-constant">IO</span>.<span class="ruby-identifier">copy_stream</span>(<span class="ruby-identifier">stdin_data</span>, <span class="ruby-identifier">i</span>)
        <span class="ruby-keyword">else</span>
          <span class="ruby-identifier">i</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">stdin_data</span>
        <span class="ruby-keyword">end</span>
      <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">EPIPE</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">i</span>.<span class="ruby-identifier">close</span>
    [<span class="ruby-identifier">outerr_reader</span>.<span class="ruby-identifier">value</span>, <span class="ruby-identifier">t</span>.<span class="ruby-identifier">value</span>]
  }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-capture3" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">capture3</span><span
                                class="method-args">(*cmd)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-capture3"><code>Open3.capture3</code></a> captures the standard output and the standard error of a command.</p>

<pre>stdout_str, stderr_str, status = Open3.capture3([env,] cmd... [, opts])</pre>

<p>The arguments env, cmd and opts are passed to <a href="Open3.html#method-c-popen3"><code>Open3.popen3</code></a> except <code>opts[:stdin_data]</code> and <code>opts[:binmode]</code>.  See Process.spawn.</p>

<p>If <code>opts[:stdin_data]</code> is specified, it is sent to the command’s standard input.</p>

<p>If <code>opts[:binmode]</code> is true, internal pipes are set to binary mode.</p>

<p>Examples:</p>

<pre class="ruby"><span class="ruby-comment"># dot is a command of graphviz.</span>
<span class="ruby-identifier">graph</span> = <span class="ruby-identifier">&lt;&lt;&#39;End&#39;</span>
<span class="ruby-value">  digraph g {
    a -&gt; b
  }
</span><span class="ruby-identifier">End</span>
<span class="ruby-identifier">drawn_graph</span>, <span class="ruby-identifier">dot_log</span> = <span class="ruby-constant">Open3</span>.<span class="ruby-identifier">capture3</span>(<span class="ruby-string">&quot;dot -v&quot;</span>, <span class="ruby-value">:stdin_data</span><span class="ruby-operator">=&gt;</span><span class="ruby-identifier">graph</span>)

<span class="ruby-identifier">o</span>, <span class="ruby-identifier">e</span>, <span class="ruby-identifier">s</span> = <span class="ruby-constant">Open3</span>.<span class="ruby-identifier">capture3</span>(<span class="ruby-string">&quot;echo abc; sort &gt;&amp;2&quot;</span>, <span class="ruby-value">:stdin_data</span><span class="ruby-operator">=&gt;</span><span class="ruby-string">&quot;foo\nbar\nbaz\n&quot;</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">o</span> <span class="ruby-comment">#=&gt; &quot;abc\n&quot;</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span> <span class="ruby-comment">#=&gt; &quot;bar\nbaz\nfoo\n&quot;</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s</span> <span class="ruby-comment">#=&gt; #&lt;Process::Status: pid 32682 exit 0&gt;</span>

<span class="ruby-comment"># generate a thumbnail image using the convert command of ImageMagick.</span>
<span class="ruby-comment"># However, if the image is really stored in a file,</span>
<span class="ruby-comment"># system(&quot;convert&quot;, &quot;-thumbnail&quot;, &quot;80&quot;, &quot;png:#{filename}&quot;, &quot;png:-&quot;) is better</span>
<span class="ruby-comment"># because of reduced memory consumption.</span>
<span class="ruby-comment"># But if the image is stored in a DB or generated by the gnuplot Open3.capture2 example,</span>
<span class="ruby-comment"># Open3.capture3 should be considered.</span>
<span class="ruby-comment">#</span>
<span class="ruby-identifier">image</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span>(<span class="ruby-string">&quot;/usr/share/openclipart/png/animals/mammals/sheep-md-v0.1.png&quot;</span>, <span class="ruby-value">:binmode</span><span class="ruby-operator">=&gt;</span><span class="ruby-keyword">true</span>)
<span class="ruby-identifier">thumbnail</span>, <span class="ruby-identifier">err</span>, <span class="ruby-identifier">s</span> = <span class="ruby-constant">Open3</span>.<span class="ruby-identifier">capture3</span>(<span class="ruby-string">&quot;convert -thumbnail 80 png:- png:-&quot;</span>, <span class="ruby-value">:stdin_data</span><span class="ruby-operator">=&gt;</span><span class="ruby-identifier">image</span>, <span class="ruby-value">:binmode</span><span class="ruby-operator">=&gt;</span><span class="ruby-keyword">true</span>)
<span class="ruby-keyword">if</span> <span class="ruby-identifier">s</span>.<span class="ruby-identifier">success?</span>
  <span class="ruby-constant">STDOUT</span>.<span class="ruby-identifier">binmode</span>; <span class="ruby-identifier">print</span> <span class="ruby-identifier">thumbnail</span>
<span class="ruby-keyword">end</span>
</pre>

                              <div class="method-source-code" id="capture3-source">
            <pre><span class="ruby-comment"># File open3.rb, line 280</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">capture3</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">stdin_data</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:stdin_data</span>) <span class="ruby-operator">||</span> <span class="ruby-string">&#39;&#39;</span>
  <span class="ruby-identifier">binmode</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:binmode</span>)

  <span class="ruby-identifier">popen3</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>, <span class="ruby-identifier">opts</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">o</span>, <span class="ruby-identifier">e</span>, <span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">binmode</span>
      <span class="ruby-identifier">i</span>.<span class="ruby-identifier">binmode</span>
      <span class="ruby-identifier">o</span>.<span class="ruby-identifier">binmode</span>
      <span class="ruby-identifier">e</span>.<span class="ruby-identifier">binmode</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">out_reader</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> { <span class="ruby-identifier">o</span>.<span class="ruby-identifier">read</span> }
    <span class="ruby-identifier">err_reader</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> { <span class="ruby-identifier">e</span>.<span class="ruby-identifier">read</span> }
    <span class="ruby-keyword">begin</span>
      <span class="ruby-keyword">if</span> <span class="ruby-identifier">stdin_data</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:readpartial</span>
        <span class="ruby-constant">IO</span>.<span class="ruby-identifier">copy_stream</span>(<span class="ruby-identifier">stdin_data</span>, <span class="ruby-identifier">i</span>)
      <span class="ruby-keyword">else</span>
        <span class="ruby-identifier">i</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">stdin_data</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">EPIPE</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">i</span>.<span class="ruby-identifier">close</span>
    [<span class="ruby-identifier">out_reader</span>.<span class="ruby-identifier">value</span>, <span class="ruby-identifier">err_reader</span>.<span class="ruby-identifier">value</span>, <span class="ruby-identifier">t</span>.<span class="ruby-identifier">value</span>]
  }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-pipeline" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">pipeline</span><span
                                class="method-args">(*cmds)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-pipeline"><code>Open3.pipeline</code></a> starts a list of commands as a pipeline. It waits for the completion of the commands. No pipes are created for stdin of the first command and stdout of the last command.</p>

<pre>status_list = Open3.pipeline(cmd1, cmd2, ... [, opts])</pre>

<p>Each cmd is a string or an array. If it is an array, the elements are passed to Process.spawn.</p>

<pre>cmd:
  commandline                              command line string which is passed to a shell
  [env, commandline, opts]                 command line string which is passed to a shell
  [env, cmdname, arg1, ..., opts]          command name and one or more arguments (no shell)
  [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)

Note that env and opts are optional, as Process.spawn.</pre>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">fname</span> = <span class="ruby-string">&quot;/usr/share/man/man1/ruby.1.gz&quot;</span>
<span class="ruby-identifier">p</span> <span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline</span>([<span class="ruby-string">&quot;zcat&quot;</span>, <span class="ruby-identifier">fname</span>], <span class="ruby-string">&quot;nroff -man&quot;</span>, <span class="ruby-string">&quot;less&quot;</span>)
<span class="ruby-comment">#=&gt; [#&lt;Process::Status: pid 11817 exit 0&gt;,</span>
<span class="ruby-comment">#    #&lt;Process::Status: pid 11820 exit 0&gt;,</span>
<span class="ruby-comment">#    #&lt;Process::Status: pid 11828 exit 0&gt;]</span>

<span class="ruby-identifier">fname</span> = <span class="ruby-string">&quot;/usr/share/man/man1/ls.1.gz&quot;</span>
<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline</span>([<span class="ruby-string">&quot;zcat&quot;</span>, <span class="ruby-identifier">fname</span>], <span class="ruby-string">&quot;nroff -man&quot;</span>, <span class="ruby-string">&quot;colcrt&quot;</span>)

<span class="ruby-comment"># convert PDF to PS and send to a printer by lpr</span>
<span class="ruby-identifier">pdf_file</span> = <span class="ruby-string">&quot;paper.pdf&quot;</span>
<span class="ruby-identifier">printer</span> = <span class="ruby-string">&quot;printer-name&quot;</span>
<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline</span>([<span class="ruby-string">&quot;pdftops&quot;</span>, <span class="ruby-identifier">pdf_file</span>, <span class="ruby-string">&quot;-&quot;</span>],
               [<span class="ruby-string">&quot;lpr&quot;</span>, <span class="ruby-node">&quot;-P#{printer}&quot;</span>])

<span class="ruby-comment"># count lines</span>
<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline</span>(<span class="ruby-string">&quot;sort&quot;</span>, <span class="ruby-string">&quot;uniq -c&quot;</span>, <span class="ruby-value">:in</span><span class="ruby-operator">=&gt;</span><span class="ruby-string">&quot;names.txt&quot;</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=&gt;</span><span class="ruby-string">&quot;count&quot;</span>)

<span class="ruby-comment"># cyclic pipeline</span>
<span class="ruby-identifier">r</span>,<span class="ruby-identifier">w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
<span class="ruby-identifier">w</span>.<span class="ruby-identifier">print</span> <span class="ruby-string">&quot;ibase=14\n10\n&quot;</span>
<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline</span>(<span class="ruby-string">&quot;bc&quot;</span>, <span class="ruby-string">&quot;tee /dev/tty&quot;</span>, <span class="ruby-value">:in</span><span class="ruby-operator">=&gt;</span><span class="ruby-identifier">r</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=&gt;</span><span class="ruby-identifier">w</span>)
<span class="ruby-comment">#=&gt; 14</span>
<span class="ruby-comment">#   18</span>
<span class="ruby-comment">#   22</span>
<span class="ruby-comment">#   30</span>
<span class="ruby-comment">#   42</span>
<span class="ruby-comment">#   58</span>
<span class="ruby-comment">#   78</span>
<span class="ruby-comment">#   106</span>
<span class="ruby-comment">#   202</span>
</pre>

                              <div class="method-source-code" id="pipeline-source">
            <pre><span class="ruby-comment"># File open3.rb, line 688</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pipeline</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmds</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">pipeline_run</span>(<span class="ruby-identifier">cmds</span>, <span class="ruby-identifier">opts</span>, [], []) {<span class="ruby-operator">|</span><span class="ruby-identifier">ts</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">ts</span>.<span class="ruby-identifier">map</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:value</span>)
  }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-pipeline_r" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">pipeline_r</span><span
                                class="method-args">(*cmds, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-pipeline_r"><code>Open3.pipeline_r</code></a> starts a list of commands as a pipeline with a pipe which connects to stdout of the last command.</p>

<pre>Open3.pipeline_r(cmd1, cmd2, ... [, opts]) {|last_stdout, wait_threads|
  ...
}

last_stdout, wait_threads = Open3.pipeline_r(cmd1, cmd2, ... [, opts])
...
last_stdout.close</pre>

<p>Each cmd is a string or an array. If it is an array, the elements are passed to Process.spawn.</p>

<pre>cmd:
  commandline                              command line string which is passed to a shell
  [env, commandline, opts]                 command line string which is passed to a shell
  [env, cmdname, arg1, ..., opts]          command name and one or more arguments (no shell)
  [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)

Note that env and opts are optional, as for Process.spawn.</pre>

<p>Example:</p>

<pre>Open3.pipeline_r(&quot;zcat /var/log/apache2/access.log.*.gz&quot;,
                 [{&quot;LANG&quot;=&gt;&quot;C&quot;}, &quot;grep&quot;, &quot;GET /favicon.ico&quot;],
                 &quot;logresolve&quot;) {|o, ts|
  o.each_line {|line|
    ...
  }
}

Open3.pipeline_r(&quot;yes&quot;, &quot;head -10&quot;) {|o, ts|
  p o.read      #=&gt; &quot;y\ny\ny\ny\ny\ny\ny\ny\ny\ny\n&quot;
  p ts[0].value #=&gt; #&lt;Process::Status: pid 24910 SIGPIPE (signal 13)&gt;
  p ts[1].value #=&gt; #&lt;Process::Status: pid 24913 exit 0&gt;
}</pre>

                              <div class="method-source-code" id="pipeline_r-source">
            <pre><span class="ruby-comment"># File open3.rb, line 519</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pipeline_r</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmds</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">out_r</span>, <span class="ruby-identifier">out_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:out</span>] = <span class="ruby-identifier">out_w</span>

  <span class="ruby-identifier">pipeline_run</span>(<span class="ruby-identifier">cmds</span>, <span class="ruby-identifier">opts</span>, [<span class="ruby-identifier">out_w</span>], [<span class="ruby-identifier">out_r</span>], <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-pipeline_rw" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">pipeline_rw</span><span
                                class="method-args">(*cmds, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-pipeline_rw"><code>Open3.pipeline_rw</code></a> starts a list of commands as a pipeline with pipes which connect to stdin of the first command and stdout of the last command.</p>

<pre>Open3.pipeline_rw(cmd1, cmd2, ... [, opts]) {|first_stdin, last_stdout, wait_threads|
  ...
}

first_stdin, last_stdout, wait_threads = Open3.pipeline_rw(cmd1, cmd2, ... [, opts])
...
first_stdin.close
last_stdout.close</pre>

<p>Each cmd is a string or an array. If it is an array, the elements are passed to Process.spawn.</p>

<pre>cmd:
  commandline                              command line string which is passed to a shell
  [env, commandline, opts]                 command line string which is passed to a shell
  [env, cmdname, arg1, ..., opts]          command name and one or more arguments (no shell)
  [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)

Note that env and opts are optional, as for Process.spawn.</pre>

<p>The options to pass to Process.spawn are constructed by merging <code>opts</code>, the last hash element of the array, and specifications for the pipes between each of the commands.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline_rw</span>(<span class="ruby-string">&quot;tr -dc A-Za-z&quot;</span>, <span class="ruby-string">&quot;wc -c&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">o</span>, <span class="ruby-identifier">ts</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;All persons more than a mile high to leave the court.&quot;</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">close</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">o</span>.<span class="ruby-identifier">gets</span> <span class="ruby-comment">#=&gt; &quot;42\n&quot;</span>
}

<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline_rw</span>(<span class="ruby-string">&quot;sort&quot;</span>, <span class="ruby-string">&quot;cat -n&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">stdin</span>, <span class="ruby-identifier">stdout</span>, <span class="ruby-identifier">wait_thrs</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">stdin</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;foo&quot;</span>
  <span class="ruby-identifier">stdin</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;bar&quot;</span>
  <span class="ruby-identifier">stdin</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;baz&quot;</span>
  <span class="ruby-identifier">stdin</span>.<span class="ruby-identifier">close</span>     <span class="ruby-comment"># send EOF to sort.</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">stdout</span>.<span class="ruby-identifier">read</span>   <span class="ruby-comment">#=&gt; &quot;     1\tbar\n     2\tbaz\n     3\tfoo\n&quot;</span>
}
</pre>

                              <div class="method-source-code" id="pipeline_rw-source">
            <pre><span class="ruby-comment"># File open3.rb, line 463</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pipeline_rw</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmds</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">in_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:in</span>] = <span class="ruby-identifier">in_r</span>
  <span class="ruby-identifier">in_w</span>.<span class="ruby-identifier">sync</span> = <span class="ruby-keyword">true</span>

  <span class="ruby-identifier">out_r</span>, <span class="ruby-identifier">out_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:out</span>] = <span class="ruby-identifier">out_w</span>

  <span class="ruby-identifier">pipeline_run</span>(<span class="ruby-identifier">cmds</span>, <span class="ruby-identifier">opts</span>, [<span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">out_w</span>], [<span class="ruby-identifier">in_w</span>, <span class="ruby-identifier">out_r</span>], <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-pipeline_start" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">pipeline_start</span><span
                                class="method-args">(*cmds, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-pipeline_start"><code>Open3.pipeline_start</code></a> starts a list of commands as a pipeline. No pipes are created for stdin of the first command and stdout of the last command.</p>

<pre>Open3.pipeline_start(cmd1, cmd2, ... [, opts]) {|wait_threads|
  ...
}

wait_threads = Open3.pipeline_start(cmd1, cmd2, ... [, opts])
...</pre>

<p>Each cmd is a string or an array. If it is an array, the elements are passed to Process.spawn.</p>

<pre>cmd:
  commandline                              command line string which is passed to a shell
  [env, commandline, opts]                 command line string which is passed to a shell
  [env, cmdname, arg1, ..., opts]          command name and one or more arguments (no shell)
  [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)

Note that env and opts are optional, as for Process.spawn.</pre>

<p>Example:</p>

<pre class="ruby"><span class="ruby-comment"># Run xeyes in 10 seconds.</span>
<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline_start</span>(<span class="ruby-string">&quot;xeyes&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">ts</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">sleep</span> <span class="ruby-value">10</span>
  <span class="ruby-identifier">t</span> = <span class="ruby-identifier">ts</span>[<span class="ruby-value">0</span>]
  <span class="ruby-constant">Process</span>.<span class="ruby-identifier">kill</span>(<span class="ruby-string">&quot;TERM&quot;</span>, <span class="ruby-identifier">t</span>.<span class="ruby-identifier">pid</span>)
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">t</span>.<span class="ruby-identifier">value</span> <span class="ruby-comment">#=&gt; #&lt;Process::Status: pid 911 SIGTERM (signal 15)&gt;</span>
}

<span class="ruby-comment"># Convert pdf to ps and send it to a printer.</span>
<span class="ruby-comment"># Collect error message of pdftops and lpr.</span>
<span class="ruby-identifier">pdf_file</span> = <span class="ruby-string">&quot;paper.pdf&quot;</span>
<span class="ruby-identifier">printer</span> = <span class="ruby-string">&quot;printer-name&quot;</span>
<span class="ruby-identifier">err_r</span>, <span class="ruby-identifier">err_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline_start</span>([<span class="ruby-string">&quot;pdftops&quot;</span>, <span class="ruby-identifier">pdf_file</span>, <span class="ruby-string">&quot;-&quot;</span>],
                     [<span class="ruby-string">&quot;lpr&quot;</span>, <span class="ruby-node">&quot;-P#{printer}&quot;</span>],
                     <span class="ruby-value">:err</span><span class="ruby-operator">=&gt;</span><span class="ruby-identifier">err_w</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">ts</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">err_w</span>.<span class="ruby-identifier">close</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">err_r</span>.<span class="ruby-identifier">read</span> <span class="ruby-comment"># error messages of pdftops and lpr.</span>
}
</pre>

                              <div class="method-source-code" id="pipeline_start-source">
            <pre><span class="ruby-comment"># File open3.rb, line 620</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pipeline_start</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmds</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">block</span>
    <span class="ruby-identifier">pipeline_run</span>(<span class="ruby-identifier">cmds</span>, <span class="ruby-identifier">opts</span>, [], [], <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">ts</span>, = <span class="ruby-identifier">pipeline_run</span>(<span class="ruby-identifier">cmds</span>, <span class="ruby-identifier">opts</span>, [], [])
    <span class="ruby-identifier">ts</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-pipeline_w" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">pipeline_w</span><span
                                class="method-args">(*cmds, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-pipeline_w"><code>Open3.pipeline_w</code></a> starts a list of commands as a pipeline with a pipe which connects to stdin of the first command.</p>

<pre>Open3.pipeline_w(cmd1, cmd2, ... [, opts]) {|first_stdin, wait_threads|
  ...
}

first_stdin, wait_threads = Open3.pipeline_w(cmd1, cmd2, ... [, opts])
...
first_stdin.close</pre>

<p>Each cmd is a string or an array. If it is an array, the elements are passed to Process.spawn.</p>

<pre>cmd:
  commandline                              command line string which is passed to a shell
  [env, commandline, opts]                 command line string which is passed to a shell
  [env, cmdname, arg1, ..., opts]          command name and one or more arguments (no shell)
  [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)

Note that env and opts are optional, as for Process.spawn.</pre>

<p>Example:</p>

<pre class="ruby"><span class="ruby-constant">Open3</span>.<span class="ruby-identifier">pipeline_w</span>(<span class="ruby-string">&quot;bzip2 -c&quot;</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=&gt;</span><span class="ruby-string">&quot;/tmp/hello.bz2&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">ts</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;hello&quot;</span>
}
</pre>

                              <div class="method-source-code" id="pipeline_w-source">
            <pre><span class="ruby-comment"># File open3.rb, line 561</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pipeline_w</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmds</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmds</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">in_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:in</span>] = <span class="ruby-identifier">in_r</span>
  <span class="ruby-identifier">in_w</span>.<span class="ruby-identifier">sync</span> = <span class="ruby-keyword">true</span>

  <span class="ruby-identifier">pipeline_run</span>(<span class="ruby-identifier">cmds</span>, <span class="ruby-identifier">opts</span>, [<span class="ruby-identifier">in_r</span>], [<span class="ruby-identifier">in_w</span>], <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-popen2" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">popen2</span><span
                                class="method-args">(*cmd, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-popen2"><code>Open3.popen2</code></a> is similar to <a href="Open3.html#method-c-popen3"><code>Open3.popen3</code></a> except that it doesn’t create a pipe for the standard error stream.</p>

<p>Block form:</p>

<pre>Open3.popen2([env,] cmd... [, opts]) {|stdin, stdout, wait_thr|
  pid = wait_thr.pid # pid of the started process.
  ...
  exit_status = wait_thr.value # Process::Status object returned.
}</pre>

<p>Non-block form:</p>

<pre>stdin, stdout, wait_thr = Open3.popen2([env,] cmd... [, opts])
...
stdin.close  # stdin and stdout should be closed explicitly in this form.
stdout.close</pre>

<p>See Process.spawn for the optional hash arguments <em>env</em> and <em>opts</em>.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-constant">Open3</span>.<span class="ruby-identifier">popen2</span>(<span class="ruby-string">&quot;wc -c&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>,<span class="ruby-identifier">o</span>,<span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">print</span> <span class="ruby-string">&quot;answer to life the universe and everything&quot;</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">close</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">o</span>.<span class="ruby-identifier">gets</span> <span class="ruby-comment">#=&gt; &quot;42\n&quot;</span>
}

<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">popen2</span>(<span class="ruby-string">&quot;bc -q&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>,<span class="ruby-identifier">o</span>,<span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;obase=13&quot;</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;6 * 9&quot;</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">o</span>.<span class="ruby-identifier">gets</span> <span class="ruby-comment">#=&gt; &quot;42\n&quot;</span>
}

<span class="ruby-constant">Open3</span>.<span class="ruby-identifier">popen2</span>(<span class="ruby-string">&quot;dc&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>,<span class="ruby-identifier">o</span>,<span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">print</span> <span class="ruby-string">&quot;42P&quot;</span>
  <span class="ruby-identifier">i</span>.<span class="ruby-identifier">close</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">o</span>.<span class="ruby-identifier">read</span> <span class="ruby-comment">#=&gt; &quot;*&quot;</span>
}
</pre>

                              <div class="method-source-code" id="popen2-source">
            <pre><span class="ruby-comment"># File open3.rb, line 147</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">popen2</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">in_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:in</span>] = <span class="ruby-identifier">in_r</span>
  <span class="ruby-identifier">in_w</span>.<span class="ruby-identifier">sync</span> = <span class="ruby-keyword">true</span>

  <span class="ruby-identifier">out_r</span>, <span class="ruby-identifier">out_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:out</span>] = <span class="ruby-identifier">out_w</span>

  <span class="ruby-identifier">popen_run</span>(<span class="ruby-identifier">cmd</span>, <span class="ruby-identifier">opts</span>, [<span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">out_w</span>], [<span class="ruby-identifier">in_w</span>, <span class="ruby-identifier">out_r</span>], <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-popen2e" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">popen2e</span><span
                                class="method-args">(*cmd, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p><a href="Open3.html#method-c-popen2e"><code>Open3.popen2e</code></a> is similar to <a href="Open3.html#method-c-popen3"><code>Open3.popen3</code></a> except that it merges the standard output stream and the standard error stream.</p>

<p>Block form:</p>

<pre>Open3.popen2e([env,] cmd... [, opts]) {|stdin, stdout_and_stderr, wait_thr|
  pid = wait_thr.pid # pid of the started process.
  ...
  exit_status = wait_thr.value # Process::Status object returned.
}</pre>

<p>Non-block form:</p>

<pre>stdin, stdout_and_stderr, wait_thr = Open3.popen2e([env,] cmd... [, opts])
...
stdin.close  # stdin and stdout_and_stderr should be closed explicitly in this form.
stdout_and_stderr.close</pre>

<p>See Process.spawn for the optional hash arguments <em>env</em> and <em>opts</em>.</p>

<p>Example:</p>

<pre># check gcc warnings
source = &quot;foo.c&quot;
Open3.popen2e(&quot;gcc&quot;, &quot;-Wall&quot;, source) {|i,oe,t|
  oe.each {|line|
    if /warning/ =~ line
      ...
    end
  }
}</pre>

                              <div class="method-source-code" id="popen2e-source">
            <pre><span class="ruby-comment"># File open3.rb, line 196</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">popen2e</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">in_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:in</span>] = <span class="ruby-identifier">in_r</span>
  <span class="ruby-identifier">in_w</span>.<span class="ruby-identifier">sync</span> = <span class="ruby-keyword">true</span>

  <span class="ruby-identifier">out_r</span>, <span class="ruby-identifier">out_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[[<span class="ruby-value">:out</span>, <span class="ruby-value">:err</span>]] = <span class="ruby-identifier">out_w</span>

  <span class="ruby-identifier">popen_run</span>(<span class="ruby-identifier">cmd</span>, <span class="ruby-identifier">opts</span>, [<span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">out_w</span>], [<span class="ruby-identifier">in_w</span>, <span class="ruby-identifier">out_r</span>], <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">ensure</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">block</span>
    <span class="ruby-identifier">in_r</span>.<span class="ruby-identifier">close</span>
    <span class="ruby-identifier">in_w</span>.<span class="ruby-identifier">close</span>
    <span class="ruby-identifier">out_r</span>.<span class="ruby-identifier">close</span>
    <span class="ruby-identifier">out_w</span>.<span class="ruby-identifier">close</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-popen3" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">popen3</span><span
                                class="method-args">(*cmd, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Open stdin, stdout, and stderr streams and start external executable. In addition, a thread to wait for the started process is created. The thread has a pid method and a thread variable :pid which is the pid of the started process.</p>

<p>Block form:</p>

<pre>Open3.popen3([env,] cmd... [, opts]) {|stdin, stdout, stderr, wait_thr|
  pid = wait_thr.pid # pid of the started process.
  ...
  exit_status = wait_thr.value # Process::Status object returned.
}</pre>

<p>Non-block form:</p>

<pre>stdin, stdout, stderr, wait_thr = Open3.popen3([env,] cmd... [, opts])
pid = wait_thr[:pid]  # pid of the started process
...
stdin.close  # stdin, stdout and stderr should be closed explicitly in this form.
stdout.close
stderr.close
exit_status = wait_thr.value  # Process::Status object returned.</pre>

<p>The parameters env, cmd, and opts are passed to Process.spawn. A commandline string and a list of argument strings can be accepted as follows:</p>

<pre>Open3.popen3(&quot;echo abc&quot;) {|i, o, e, t| ... }
Open3.popen3(&quot;echo&quot;, &quot;abc&quot;) {|i, o, e, t| ... }
Open3.popen3([&quot;echo&quot;, &quot;argv0&quot;], &quot;abc&quot;) {|i, o, e, t| ... }</pre>

<p>If the last parameter, opts, is a Hash, it is recognized as an option for Process.spawn.</p>

<pre class="ruby"><span class="ruby-constant">Open3</span>.<span class="ruby-identifier">popen3</span>(<span class="ruby-string">&quot;pwd&quot;</span>, <span class="ruby-value">:chdir</span><span class="ruby-operator">=&gt;</span><span class="ruby-string">&quot;/&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>,<span class="ruby-identifier">o</span>,<span class="ruby-identifier">e</span>,<span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">o</span>.<span class="ruby-identifier">read</span>.<span class="ruby-identifier">chomp</span> <span class="ruby-comment">#=&gt; &quot;/&quot;</span>
}
</pre>

<p>wait_thr.value waits for the termination of the process. The block form also waits for the process when it returns.</p>

<p>Closing stdin, stdout and stderr does not wait for the process to complete.</p>

<p>You should be careful to avoid deadlocks. Since pipes are fixed length buffers, <a href="Open3.html#method-c-popen3"><code>Open3.popen3</code></a>(“prog”) {|i, o, e, t| o.read } deadlocks if the program generates too much output on stderr. You should read stdout and stderr simultaneously (using threads or IO.select). However, if you don’t need stderr output, you can use <a href="Open3.html#method-c-popen2"><code>Open3.popen2</code></a>. If merged stdout and stderr output is not a problem, you can use <a href="Open3.html#method-c-popen2e"><code>Open3.popen2e</code></a>. If you really need stdout and stderr output as separate strings, you can consider <a href="Open3.html#method-c-capture3"><code>Open3.capture3</code></a>.</p>

                              <div class="method-source-code" id="popen3-source">
            <pre><span class="ruby-comment"># File open3.rb, line 86</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">popen3</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">cmd</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">last</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">opts</span> = {}
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">in_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:in</span>] = <span class="ruby-identifier">in_r</span>
  <span class="ruby-identifier">in_w</span>.<span class="ruby-identifier">sync</span> = <span class="ruby-keyword">true</span>

  <span class="ruby-identifier">out_r</span>, <span class="ruby-identifier">out_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:out</span>] = <span class="ruby-identifier">out_w</span>

  <span class="ruby-identifier">err_r</span>, <span class="ruby-identifier">err_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
  <span class="ruby-identifier">opts</span>[<span class="ruby-value">:err</span>] = <span class="ruby-identifier">err_w</span>

  <span class="ruby-identifier">popen_run</span>(<span class="ruby-identifier">cmd</span>, <span class="ruby-identifier">opts</span>, [<span class="ruby-identifier">in_r</span>, <span class="ruby-identifier">out_w</span>, <span class="ruby-identifier">err_w</span>], [<span class="ruby-identifier">in_w</span>, <span class="ruby-identifier">out_r</span>, <span class="ruby-identifier">err_r</span>], <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is a service of <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>, purveyors of fine <a href='https://jamesbritt.bandcamp.com/'>dance noise</a></p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

